﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace YXShop.Web.admin.product
{
    public partial class product_batch_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("001010004", "对不起，您没有商品批量编辑的权限");

                if (Request.Form["Option"] != null && !Request.Form["Option"].Trim().Equals(""))
                {
                    string Name = Request.Form["Name"].ToString();
                    string productCID = ChangeHope.WebPage.PageRequest.GetFormString("productCId");
                    string productBrandId = ChangeHope.WebPage.PageRequest.GetFormString("productBrandId");
                    string productNo = ChangeHope.WebPage.PageRequest.GetFormString("productNo");
                    string types = Request.Form["Option"].Trim();
                    if (types == "SearchProduct")
                    {
                        this.SearchProduct(productCID,productBrandId,Name,productNo);
                    }
                    Response.End();
                    return;
                }

                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                this.txtProductClass.Attributes.Add("readonly", "readonly");
                this.txtProductClass.Attributes.Add("onclick", "selectFile('Random_Productclass',new Array(" + this.hfcid.ClientID + "," + this.txtProductClass.ClientID + "),310,450,'" + sp.DummyPaht + "');");
                this.BindBrand();
                this.BindProduct();
            }
        }

        protected void BindBrand()
        {
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            List<YXShop.Model.Product.ProductBrand> brandList = bll.GetDTList("");
            if (brandList.Count > 0)
            {
                this.ddlProductBrand.Items.Add(new ListItem("请选择...","0"));
                for (int i = 0; i < brandList.Count; i++)
                {
                    this.ddlProductBrand.Items.Add(new ListItem(brandList[i].Name, brandList[i].ID.ToString()));
                }
            }
        }

        protected void SearchProduct(string CID,string BrandId,string ProductName,string ProductNO)
        {
            YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
            string cond = "";
            if (!string.IsNullOrEmpty(ProductName))
            {
                cond += " and pro_Name like '%" + ProductName + "%'";
            }
            if (!string.IsNullOrEmpty(ProductNO))
            {
                cond += " and pro_NO like '%" + ProductNO + "%'";
            }
            if (!string.IsNullOrEmpty(BrandId) && BrandId != "0")
            {
                cond += " and pro_BrandID=" + BrandId + "";
            }
            if (!string.IsNullOrEmpty(CID) && CID != "0")
            {
                cond += " and cid in (" + YXShop.TemplateAction.Common.CodStr(CID) + ")";
            }
            DataTable dt = bll.DTGetListWhere(cond);
            StringBuilder shtml = new StringBuilder();
            shtml.Append("<select size=\"14\" id=\"Seleted\" style=\"width: 98%\"  multiple=\"multiple\">");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    shtml.Append("<option value='" + dt.Rows[i]["pro_ID"].ToString() + "' title='" + dt.Rows[i]["pro_Name"].ToString() + "'>" + dt.Rows[i]["pro_Name"].ToString() + "</option>");
                }
            }
            shtml.Append("</select>");
            Response.Write(shtml.ToString());
        }

        protected void BindProduct()
        {
            StringBuilder shtml = new StringBuilder();
            string productId = ChangeHope.WebPage.PageRequest.GetQueryString("barchId");
            shtml.Append("<select name=\"Selects\" size=\"14\" multiple=\"multiple\" style=\"width: 98%\">");
            if (!string.IsNullOrEmpty(productId))
            {
                YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
                DataTable dt = bll.GetPartData(productId);
                if (dt.Rows.Count>0)
                {
                    for (int i = 0; i < dt.Rows.Count;i++ )
                    {
                        shtml.Append("<option value='" + dt.Rows[i]["pro_ID"].ToString() + "' title='" + dt.Rows[i]["pro_Name"].ToString() + "' selected>" + dt.Rows[i]["pro_Name"].ToString() + "</option>");
                    }
                }
               
            } 
            shtml.Append("</select>");
            this.ltProductSelected.Text = shtml.ToString();
        }

        protected void butEdit_Click(object sender, EventArgs e)
        {
            
            string editProductId = ChangeHope.WebPage.PageRequest.GetFormString("Selects");
            string editField = ChangeHope.WebPage.PageRequest.GetFormString("SelRequiredField");
            Response.Redirect("product_batch_edite.aspx?productId=" + editProductId + "&field=" + editField + "");
        }
    }
}
